import Cookies from 'js-cookie'
import {removeToken} from "@/utils/auth";


const state = {
    avatar: Cookies.get('avatar'),
    role: Cookies.get('role'),
    userId: Cookies.get('userId'),
    userName: Cookies.get('userName'),
    phone: Cookies.get('phone')
}

const getters = {

    getAvatar() {
        return state.avatar;
    },
    getRole() {
        return state.role;
    },
    getUserId() {
        return state.userId;
    }
}

const mutations = {
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar;
        Cookies.set('avatar', avatar);
    },
    SET_ROLE: (state, role) => {
        state.role = role;
        Cookies.set('role', role)
    },
    SET_USERID: (state, userId) => {
        state.userId = userId;
        Cookies.set('userId', userId)
    },
    LOG_OUT: (state) => {
        state.role = '';
        state.userId = '';
        Cookies.remove('userId')
        Cookies.remove('role')
        removeToken();
    }
}

const actions = {
    setAvatar({commit}, avatar) {
        commit('SET_AVATAR', avatar);
    },
    setRole({commit}, role) {
        commit('SET_ROLE', role)
    },
    setUserInfo({commit}, userInfo) {
        commit('SET_USERID', userInfo.id)
        commit('SET_AVATAR', userInfo.avatar);
    },
    logOut({commit}){
        commit('LOG_OUT')
    }

}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}